/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.infosystem.noeplaza.lms.struts.action;

import com.eit.ntc.spring.utils.DateTimeUtils;
import com.eit.ntc.spring.utils.RandomUtils;
import com.eit.toschoolonline.lms.constant.LMSConstant;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemHistoryM;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemViewM;
import com.eit.toschoolonline.lms.frontend.service.LearningItemHistoryService;
import com.eit.toschoolonline.lms.frontend.service.LmsViewService;
//import com.eit.toschoolonline.lms.jdo.LearningItemM;
//import com.eit.toschoolonline.lms.service.LearningItemHistoryService;
//import com.eit.toschoolonline.lms.service.LearningItemService;
//import com.eit.toschoolonline.lms.service.LmsServiceFactory;
import com.eit.toschoolonline.openid.jdo.OpenIdUserM;
import com.eit.toschoolonline.openid.jdo.OpenIdUserRoleM;
import com.eit.toschoolonline.openid.service.spring.OpenIdService;
import java.util.Calendar;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author Administrator
 */
public class ShowItemAction extends Action{
     private LmsViewService lmsService;
     private OpenIdService openIdService;
     private LearningItemHistoryService historyService;


     private OpenIdUserM selfRegister(String claim) throws Exception{
        Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
        OpenIdUserM claimUser = new OpenIdUserM();
        claimUser.setUsername(claim);
        claimUser.setPassword(RandomUtils.randomString(20));
        claimUser.setEnabled(1);
        claimUser.setLastLoginTime(calendar.getTime());


        OpenIdUserRoleM role = openIdService.getOpenIdUserRole("ROLE_OPENID_USER");

        claimUser.getAuthorities().add(role);
        claimUser = openIdService.createOpenIdUser(claimUser);
        return claimUser;
     }
     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String entType = request.getParameter("entType");
//        LearningItemService itemService = getLmsServiceFactory().getLearningItemService();
       

        String claim = request.getParameter("claim");
        if (claim==null || claim.trim().equals("")){ // no claim information
            System.out.println("No Claim Information..!!");
            return mapping.findForward("noClaimInfo");
        };
        
//        String username = request.getRemoteUser();
        OpenIdUserM openIdUser = (OpenIdUserM) request.getSession().getAttribute("OpenId");
        if (openIdUser==null){ // first contact application 
            
            // not authenticated
              System.out.println("First Contact.... to authenticated with OpenId Claim:"+claim);
               //String claim = request.getParameter("claim");
              
                    
                     openIdUser = openIdService.getOpenIdUser(claim);
                    if (openIdUser==null){ // self register
                        openIdUser = selfRegister(claim);
                        System.out.println("Unauthenticated -- Self Register with claim id: "+openIdUser.getUsername());
                    };
                    
              
            };

                   
                    if (openIdUser!=null && openIdUser.getUsername()!=null
                            && openIdUser.getUsername().trim().equals(claim.trim())){
                         System.out.println("Existing OpenId User Information:"+openIdUser.getUsername());
                        request.getSession().setAttribute("cid", openIdUser.getUsername());


                        System.out.println("redirecto to showItem.do?id="+id+"&claim="+openIdUser.getUsername());
                        String reShowItemAction = "/reShowItem.do";
                        ActionRedirect ar = new ActionRedirect(reShowItemAction);

                        ar.addParameter("id", id);
                        ar.addParameter("claim", openIdUser.getUsername());
                        return ar;
                    } else {
                        System.out.println("No OpenId User Information..!!");
                        return mapping.findForward("noOpenIdUserInfo");
                    }
            
        

            
               

     }

//    public LmsServiceFactory getLmsServiceFactory() {
//        return lmsServiceFactory;
//    }
//
//    public void setLmsServiceFactory(LmsServiceFactory lmsServiceFactory) {
//        this.lmsServiceFactory = lmsServiceFactory;
//    }

    public LearningItemHistoryService getHistoryService() {
        return historyService;
    }

    public void setHistoryService(LearningItemHistoryService historyService) {
        this.historyService = historyService;
    }

    public LmsViewService getLmsService() {
        return lmsService;
    }

    public void setLmsService(LmsViewService lmsService) {
        this.lmsService = lmsService;
    }

    /**
     * @return the openIdService
     */
    public OpenIdService getOpenIdService() {
        return openIdService;
    }

    /**
     * @param openIdService the openIdService to set
     */
    public void setOpenIdService(OpenIdService openIdService) {
        this.openIdService = openIdService;
    }
}
